<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>权限增加</title>
    <link rel="stylesheet" href="/js/bootstrap.min.css">
    <script src="/js/jquery-3.2.1.min.js"></script>
    <script src="/js/bootstrap.min.js"></script>
    <script type="text/javascript">
        $(function () {
            //调用获得参数方法
            getParam();

            //获得参数的方法
            function getUrlParam(name) {
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
                var r = window.location.search.substr(1).match(reg);  //匹配目标参数
                if (r != null)
                    return unescape(r[2]);
                return null; //返回参数值
            };
            var id = null;


            function getParam() {
                id = getUrlParam("id");
                $("#ids").attr("value", id);
            };

            var roleForm = $("#roleForm")

            //展示权限
            function showRight() {
                $.get("/applets/menu/getAll", function (da) {
                    if (da.code != 0) {
                        alert(da.msg);
                        return;
                    }
                    $.each(da.data, function (i, d) {
                        roleForm.append("<div class='form-check'><label class='form-check-label'><input type='checkbox' class='form-check-input' value='" +
                            d.id + "'>" + d.mtext + "</label></label></div>")
                    });
                });
            };
            showRight();

            var pro = null;

            //ajax修改状态
            function submit() {
                //选中为空不行
                if ($('input:checkbox:checked').length == 0) {
                    alert("请选择要增加的权限!")
                    return;
                }
                //新建数组,插入值
                pro = new Array();
                $('input:checkbox:checked').each(function () {
                    pro.push($(this).val());
                })
                //改为字符串,方便传参
                var prostr = pro.join("-");

                $.get("/Kshop/menupriv/add", {"prostr": prostr, "id": id}, function (data) {
                    alert(data.msg);
                })
            };


            $("#button").click(function () {
                id = $("#ids").val();
                submit();
            });

        });
    </script>
</head>
<body>
<div class="container">
    <h2 class="text-center">添加权限</h2>

    <!--导航-->
    <ul class="nav nav-tabs">
        <li class="nav-item">
            <a class="nav-link active" href="/KRole/ShowRole.html">返回展示界面</a>
        </li>
    </ul>


    <form action="#" method="post" id="roleForm">
        <div class="form-group">
            <input type="text" class="form-control" name="id" id="ids" readonly hidden>
        </div>
    </form>
    <div class="form-group">
        <input type="button" class="form-control" id="button" value="确认添加">
    </div>
</div>

</body>
</html>